﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit
{
    public partial class UCMagicData : UserControl
    {
        List<Magic> magics;

        PListRoot root;
        String fileName;
        String resourcesDirect = Form1.cczDirector+"\\";
        public UCMagicData()
        {
            InitializeComponent();
        }
        public UCMagicData(String cczDirector)
        {
            InitializeComponent();
            listView1.Columns.Add("ID", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("名称", 120, HorizontalAlignment.Left);
            fileName = cczDirector+"\\MagicData.plist";
            magics = new List<Magic>();
            LoadData(fileName);
            magics.Sort(Magic.compare);
            foreach (KeyValuePair<int, String> kv in Constant.magicTypeDic)
            {
                comboBoxType.Items.Add(kv.Value);
            }
            foreach (KeyValuePair<int, String> kv in Constant.magicEffectDic)
            {
                comboBoxNumEffect.Items.Add(kv.Value);
            }
            foreach (KeyValuePair<int, String> kv in Constant.markTypeDic)
            {
                comboBoxRangeAttack.Items.Add(kv.Value);
            } foreach (KeyValuePair<int, String> kv in Constant.markEffectTypeDic)
            {
                comboBoxRangeEffect.Items.Add(kv.Value);
            }
            ShowMagics();
        }
        private void LoadData(String fileName_)
        {

            root = PListRoot.Load(fileName_);
            PListDict rootDic = (PListDict)root.Root;
            PListDict magicsDic = (PListDict)rootDic["Magics"];
            foreach (KeyValuePair<String, IPListElement> a in magicsDic)
            {
                PListDict magicDic = (PListDict)a.Value;
                PListString name = (PListString)magicDic["Name"];
                PListInteger num = (PListInteger)magicDic["Num"];
                PListInteger numIcon = (PListInteger)magicDic["NumIcon"];
                PListString dspt = (PListString)magicDic["Dspt"];
                PListInteger numEffect = (PListInteger)magicDic["NumEffect"];
                PListInteger type = (PListInteger)magicDic["Type"];
                PListInteger target = (PListInteger)magicDic["Target"];
                PListInteger consume = (PListInteger)magicDic["Consume"];
                PListInteger numAnimCall = (PListInteger)magicDic["NumAnimCall"];
                PListInteger numAnimMeff = (PListInteger)magicDic["NumAnimMeff"];
                PListInteger numAnimSeff = (PListInteger)magicDic["NumAnimSeff"];
                PListInteger numRangeAttack = (PListInteger)magicDic["NumRangeAttack"];
                PListInteger numRangeEffect = (PListInteger)magicDic["NumRangeEffect"];
                PListInteger valueAlpha = (PListInteger)magicDic["ValueAlpha"];
                PListInteger valueC = (PListInteger)magicDic["ValueC"];

                Magic magic = new Magic();
                magic.name = name.Value;
                magic.num = num.Value;
                magic.dspt = dspt.Value;
                magic.numEffect = numEffect.Value;
                magic.numIcon = Convert.ToInt64(numIcon.Value);
                magic.type = type.Value;
                magic.target = target.Value;
                magic.consume = consume.Value;
                magic.numAnimCall = numAnimCall.Value;
                magic.numAnimMeff = numAnimMeff.Value;
                magic.numAnimSeff = numAnimSeff.Value;
                magic.numRangeAttack = numRangeAttack.Value;
                magic.numRangeEffect = numRangeEffect.Value;
                magic.valueAlpha = valueAlpha.Value;
                magic.valueC = valueC.Value;

                magics.Add(magic);

            }

        }
        private void ShowMagics()
        {
            listView1.Clear();
            listView1.Columns.Add("ID", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("名称", 120, HorizontalAlignment.Left);


            foreach (Magic magic in magics)
            {
                ListViewItem item = new ListViewItem(new String[] { magic.num + "", magic.name });
                listView1.Items.Add(item);
            }

            listView1.Focus();
            listView1.Items[0].Selected = true;

        }
        void ShowMagicDetail(int ID)
        {
            Magic magic= magics.Find((m)=>{return m.num==ID;});

            textBoxName.Text = magic.name;
            textBoxConsume.Text = magic.consume.ToString();
            textBoxTarget.Text = magic.target.ToString();
            comboBoxType.Text = Constant.magicTypeDic[(int)magic.type];
            comboBoxNumEffect.Text = Constant.magicEffectDic[(int)magic.numEffect];
            textBoxAlpha.Text = magic.valueAlpha.ToString();
            textBoxC.Text = magic.valueC.ToString();
            textBoxDspt.Text = magic.dspt;
            numericUpDownIcon.Value = magic.numIcon;
            numericUpDownNumAnimCall.Value = magic.numAnimCall;
            numericUpDownNumAnimMeff.Value = magic.numAnimMeff;
            numericUpDownNumAnimSeff.Value = magic.numAnimSeff;

            comboBoxRangeAttack.Text = Constant.markTypeDic[(int)magic.numRangeAttack];
            comboBoxRangeEffect.Text = Constant.markEffectTypeDic[(int)magic.numRangeEffect];


        }
        public void SaveAllData()
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            SaveMagicDetail(Convert.ToInt32(item.SubItems[0].Text));
            //PListDict rootDic = (PListDict)root.Root;

            //PListDict magicsDic = (PListDict)rootDic["Magics"];
            //foreach (KeyValuePair<String, IPListElement> a in magicsDic)
            //{
            //    PListDict magicDic = (PListDict)a.Value;
            //    magicDic["NumAnimCall"] = new PListInteger(-1);
            //    magicDic["NumAnimMeff"] = new PListInteger(-1);
            //    magicDic["NumAnimSeff"] = new PListInteger(-1);
            //}
            root.Save(fileName);

        }


        void SaveMagicDetail(int index)
        {
            Magic magic = magics.Find((h) => { return h.num == index; });
            magic.name = textBoxName.Text;
            magic.dspt = textBoxDspt.Text;
            magic.consume = Convert.ToInt64(textBoxConsume.Text);
            magic.target = Convert.ToInt64(textBoxTarget.Text);
            magic.valueAlpha = Convert.ToInt64(textBoxAlpha.Text);
            magic.valueC = Convert.ToInt64(textBoxC.Text);
            magic.numIcon = Convert.ToInt64(numericUpDownIcon.Value);
            magic.numAnimCall = Convert.ToInt64(numericUpDownNumAnimCall.Value);
            magic.numAnimMeff = Convert.ToInt64(numericUpDownNumAnimMeff.Value);
            magic.numAnimSeff = Convert.ToInt64(numericUpDownNumAnimSeff.Value);
            foreach (KeyValuePair<int, String> kv in Constant.magicTypeDic)
            {
                if (kv.Value == comboBoxType.Text)
                {
                    magic.type = kv.Key;
                }
            }
            foreach (KeyValuePair<int, String> kv in Constant.magicEffectDic)
            {
                if (kv.Value == comboBoxNumEffect.Text)
                {
                    magic.numEffect = kv.Key;
                }
            } 
            foreach (KeyValuePair<int, String> kv in Constant.markTypeDic)
            {
                if (kv.Value == comboBoxRangeAttack.Text)
                {
                    magic.numRangeAttack = kv.Key;
                }
            } 
            foreach (KeyValuePair<int, String> kv in Constant.markEffectTypeDic)
            {
                if (kv.Value == comboBoxRangeEffect.Text)
                {
                    magic.numRangeEffect = kv.Key;
                }
            }
            PListDict rootDic = (PListDict)root.Root;
            PListDict magicsDic = (PListDict)rootDic["Magics"];
            foreach (KeyValuePair<String, IPListElement> a in magicsDic)
            {
                PListDict magicDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)magicDic["Num"];
                if (num.Value == magic.num)
                {
                    magicDic["Name"] = new PListString(magic.name);
                    magicDic["Dspt"] = new PListString(magic.dspt);
                    magicDic["NumEffect"] = new PListInteger(magic.numEffect);
                    magicDic["NumIcon"] = new PListInteger(magic.numIcon);
                    magicDic["Type"] = new PListInteger(magic.type);
                    magicDic["Target"] = new PListInteger(magic.target);
                    magicDic["Consume"] = new PListInteger(magic.consume);
                    magicDic["NumAnimCall"] = new PListInteger(magic.numAnimCall);
                    magicDic["NumAnimMeff"] = new PListInteger(magic.numAnimMeff);
                    magicDic["NumAnimSeff"] = new PListInteger(magic.numAnimSeff);
                    magicDic["NumRangeAttack"] = new PListInteger(magic.numRangeAttack);
                    magicDic["NumRangeEffect"] = new PListInteger(magic.numRangeEffect);
                    magicDic["ValueAlpha"] = new PListInteger(magic.valueAlpha);
                    magicDic["ValueC"] = new PListInteger(magic.valueC);
                }
            }
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            ShowMagicDetail(Convert.ToInt32(item.SubItems[0].Text));

        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
                SaveMagicDetail(Convert.ToInt32(item.SubItems[0].Text));
        }

        private void buttonRecover_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];

                ShowMagicDetail(Convert.ToInt32(item.SubItems[0].Text));
        }

        private void numericUpDownIcon_ValueChanged(object sender, EventArgs e)
        {
            pictureBoxIcon.Image = Image.FromFile(resourcesDirect + "Item\\Item" + numericUpDownIcon.Value + ".png");
        }

        private void comboBoxRangeAttack_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (KeyValuePair<int, String> kv in Constant.markTypeDic)
            {
                if (kv.Value == comboBoxRangeAttack.Text)
                {
                    pictureBoxAttackRange.Image = Image.FromFile(resourcesDirect + "Hitarea\\HitArea" + kv.Key + ".png");
                    
                }
            }

        }

        private void comboBoxRangeEffect_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (KeyValuePair<int, String> kv in Constant.markEffectTypeDic)
            {
                if (kv.Value == comboBoxRangeEffect.Text)
                {
                    Image oldImg = Image.FromFile(resourcesDirect + "Effarea\\EffArea" + kv.Key + ".png");
                    pictureBoxEffectRange.Image =
                     oldImg.GetThumbnailImage(64, 64, new Image.GetThumbnailImageAbort(isTrue), IntPtr.Zero); 
                }
            }
        }
        private static bool isTrue()
        {
            return true;
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {

            Magic magic = new Magic();
            PListDict rootDic=(PListDict)root.Root;
            PListInteger newNum = (PListInteger)rootDic["NewNum"];
            magic.num = newNum.Value;
            magic.name = textBoxName.Text;
            magic.numIcon = Convert.ToInt64(numericUpDownIcon.Value);
            magic.dspt = textBoxDspt.Text;
            magic.consume = Convert.ToInt64(textBoxConsume.Text);
            magic.target = Convert.ToInt64(textBoxTarget.Text);
            magic.valueAlpha = Convert.ToInt64(textBoxAlpha.Text);
            magic.valueC = Convert.ToInt64(textBoxC.Text);
            magic.numIcon = Convert.ToInt64(numericUpDownIcon.Value);
            magic.numAnimCall = Convert.ToInt64(numericUpDownNumAnimCall.Value);
            magic.numAnimMeff = Convert.ToInt64(numericUpDownNumAnimMeff.Value);
            magic.numAnimSeff = Convert.ToInt64(numericUpDownNumAnimSeff.Value);
            foreach (KeyValuePair<int, String> kv in Constant.magicTypeDic)
            {
                if (kv.Value == comboBoxType.Text)
                {
                    magic.type = kv.Key;
                }
            }
            foreach (KeyValuePair<int, String> kv in Constant.magicEffectDic)
            {
                if (kv.Value == comboBoxNumEffect.Text)
                {
                    magic.numEffect = kv.Key;
                }
            }
            foreach (KeyValuePair<int, String> kv in Constant.markTypeDic)
            {
                if (kv.Value == comboBoxRangeAttack.Text)
                {
                    magic.numRangeAttack = kv.Key;
                }
            }
            foreach (KeyValuePair<int, String> kv in Constant.markEffectTypeDic)
            {
                if (kv.Value == comboBoxRangeEffect.Text)
                {
                    magic.numRangeEffect = kv.Key;
                }
            }
            PListDict magicsDic = (PListDict)rootDic["Magics"];
            PListDict magicDic = new PListDict();
            magicDic["Num"] = new PListInteger(magic.num);
            magicDic["Name"] = new PListString(magic.name);
            magicDic["Dspt"] = new PListString(magic.dspt);
            magicDic["NumEffect"] = new PListInteger(magic.numEffect);
            magicDic["NumIcon"] = new PListInteger(magic.numIcon);
            magicDic["Type"] = new PListInteger(magic.type);
            magicDic["Target"] = new PListInteger(magic.target);
            magicDic["Consume"] = new PListInteger(magic.type);
            magicDic["NumAnimCall"] = new PListInteger(magic.numAnimCall);
            magicDic["NumAnimMeff"] = new PListInteger(magic.numAnimMeff);
            magicDic["NumAnimSeff"] = new PListInteger(magic.numAnimSeff);
            magicDic["NumRangeAttack"] = new PListInteger(magic.numRangeAttack);
            magicDic["NumRangeEffect"] = new PListInteger(magic.numRangeEffect);
            magicDic["ValueAlpha"] = new PListInteger(magic.valueAlpha);
            magicDic["ValueC"] = new PListInteger(magic.valueC);
            magicsDic.Add("Magic" + newNum.Value, magicDic);
            rootDic["NewNum"] = new PListInteger(magic.num+1);
            magics.Add(magic);
            ShowMagics();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listView1.SelectedItems[0];
            magics.RemoveAll((m) => { return m.num == (Convert.ToInt32(item.SubItems[0].Text)); });
            PListDict rootDic = (PListDict)root.Root;
            PListDict magicsDic = (PListDict)rootDic["Magics"];
            String magicKey = null;
            foreach (KeyValuePair<String, IPListElement> a in magicsDic)
            {
                PListDict magicDic = (PListDict)a.Value;
                PListInteger num = (PListInteger)magicDic["Num"];
                if (num.Value == Convert.ToInt32(item.SubItems[0].Text))
                {
                    magicKey = a.Key;
                }
            }
            if (magicKey != null)
                magicsDic.Remove(magicKey);
            ShowMagics();

                
        }
    }
    public class Magic {
        public String name;
        public long num;
        public String dspt;
        public long type;
        public long numEffect;
        public long consume;
        public long numAnimCall;
        public long numAnimMeff;
        public long numAnimSeff;
        public long numIcon;
        public long numRangeAttack;
        public long numRangeEffect;
        public long valueAlpha;
        public long valueC;
        public long target;
        public static int compare(Magic m1, Magic m2)
        {
            if (m1.num == m2.num)
                return 0;
            else if (m1.num > m2.num)

                return 1;

            else if (m1.num < m2.num)
                return -1;
            else
                return 0;
        }


    }
}
